package com.hb.hb_vote;

import java.sql.*;
import java.util.ArrayList;
import com.wonders.ips.common.*;

import java.text.SimpleDateFormat;
import java.util.Date;

public class Hb_Vote {

  public int id;
  public int userid;
  public int case_id;
  public int flag_good;
  public int flag_bad;
  public String add_date;

  private String sql = "";

  public Hb_Vote() {
  }

  public void add(int userid,int case_id,int flag_good,int flag_bad) {
	DbConnection dbcon = null;
	Connection conn = null;
	Statement stmt = null;
	PreparedStatement pstmt = null;
	ResultSet rs = null;
    try {
      dbcon = new DbConnection();
      conn = dbcon.getCon();

      //int i_id = dbcon.getMaxId("tb_guestbook");

      sql = "insert into hb_vote (userid,case_id,flag_good,flag_bad,add_date) " +
            "values (?,?,?,?,NOW())";
      pstmt = conn.prepareStatement(sql);

      pstmt.setInt(1,userid);
	  pstmt.setInt(2,case_id);
	  pstmt.setInt(3,flag_good);
	  pstmt.setInt(4,flag_bad);
	  
      pstmt.executeUpdate();

    } catch (Exception e) {
      System.err.println(e.getMessage());
    } finally {
        try {if (pstmt!=null) pstmt.close();}
        catch (Exception e) {}
        try {if (conn!=null) conn.close();}
        catch (Exception e) {}
    }

  }

   public void del(int i_id) {
	DbConnection dbcon = null;
	Connection conn = null;
	Statement stmt = null;
	PreparedStatement pstmt = null;
	
    try {
      dbcon = new DbConnection();
      conn = dbcon.getCon();
      sql = "delete * from hb_vote where id=? ";
      pstmt = conn.prepareStatement(sql);

      pstmt.setInt(1,i_id);
      pstmt.executeUpdate();

    } catch (Exception e) {
      System.err.println(e.getMessage());
    } finally {
        try {if (pstmt!=null) pstmt.close();}
        catch (Exception e) {}
        try {if (conn!=null) conn.close();}
        catch (Exception e) {}
    }
  }
  
  
  public void update_good(int i_id) {
	DbConnection dbcon = null;
	Connection conn = null;
	Statement stmt = null;
	PreparedStatement pstmt = null;
	
	try {
	  dbcon = new DbConnection();
	  conn = dbcon.getCon();

	  sql ="update hb_vote set flag_good=1 where id=?";

	  pstmt = conn.prepareStatement(sql);


	  pstmt.setInt(1,i_id);
	  pstmt.executeUpdate();

	} catch (Exception e) {
	  System.err.println(e.getMessage());
	} finally {
		try {if (pstmt!=null) pstmt.close();}
		catch (Exception e) {}
		try {if (conn!=null) conn.close();}
		catch (Exception e) {}
	}

  }
  
  public void update_bad(int i_id) {
		DbConnection dbcon = null;
		Connection conn = null;
		Statement stmt = null;
		PreparedStatement pstmt = null;
		
		try {
		  dbcon = new DbConnection();
		  conn = dbcon.getCon();

		  sql ="update hb_vote set flag_bad=1 where id=?";

		  pstmt = conn.prepareStatement(sql);


		  pstmt.setInt(1,i_id);
		  pstmt.executeUpdate();

		} catch (Exception e) {
		  System.err.println(e.getMessage());
		} finally {
			try {if (pstmt!=null) pstmt.close();}
			catch (Exception e) {}
			try {if (conn!=null) conn.close();}
			catch (Exception e) {}
		}

	  }

  public int findById(int case_id,int userid) throws SQLException{
		DbConnection dbcon = null;
		Connection conn = null;
		Statement stmt = null;
		ResultSet rs = null;
		int p=0;
	  try{
   dbcon = new DbConnection();
   conn = dbcon.getCon();
   stmt = conn.createStatement();
   sql = "select * from hb_vote where case_id="+case_id+" and userid="+userid;
   System.out.println(sql);
   rs = stmt.executeQuery(sql);
   if(rs.next()){    
   p=rs.getInt("flag_good");
   
   }
   return p;
      } 
		 catch (Exception e) {
   System.err.println(e.getMessage());
   return 0;
 } finally {
 	stmt.close();
			conn.close();
	  }

}
  public static void main(String[] args) {

  }


}
